﻿package baseEngine.collisions
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import baseEngine.utils.*;
    import flash.geom.*;

    public class MouseCollision extends Object
    {
        public var data:Vector.<CollisionInfo>;
        private var _ray:RayCollision;
        private var _cam:Camera3D;
        private static var _pos:Vector3D = new Vector3D();
        private static var _dir:Vector3D = new Vector3D();
        private static var _inv:Matrix3D = new Matrix3D();

        public function MouseCollision(param1:Camera3D = null)
        {
            this.data = new Vector.<CollisionInfo>;
            this._cam = param1;
            this._ray = new RayCollision();
            return;
        }// end function

        public function dispose() : void
        {
            this.data = null;
            this._cam = null;
            this._ray.dispose();
            this._ray = null;
            return;
        }// end function

        public function test(param1:Number, param2:Number, param3:Boolean = false, param4:Boolean = true, param5:Boolean = true) : Boolean
        {
            if (!this.getCameraDir(param1, param2))
            {
                return false;
            }
            this._ray.test(_pos, _dir, param3, param5);
            this.data = this._ray.data;
            return this._ray.collided;
        }// end function

        private function getCameraDir(param1:Number, param2:Number) : Boolean
        {
            var _loc_3:* = this._cam ? (this._cam) : (Global3D.camera);
            if (!Global3D.scene)
            {
                return false;
            }
            var _loc_4:* = Global3D.scene.viewPort;
            if (!_loc_3 || !_loc_4)
            {
                return false;
            }
            _loc_3.getPosition(false, _pos);
            param1 = param1 - _loc_4.x;
            param2 = param2 - _loc_4.y;
            _dir.x = (param1 / _loc_4.width - 0.5) * 2;
            _dir.y = ((-param2) / _loc_4.height + 0.5) * 2;
            _dir.z = 1;
            Matrix3DUtils.invert(_loc_3.projection, _inv);
            Matrix3DUtils.transformVector(_inv, _dir, _dir);
            _dir.x = _dir.x * _dir.z;
            _dir.y = _dir.y * _dir.z;
            Matrix3DUtils.deltaTransformVector(_loc_3.world, _dir, _dir);
            return true;
        }// end function

        public function addCollisionWith(param1:Pivot3D, param2:Boolean = true) : void
        {
            this._ray.addCollisionWith(param1, param2);
            return;
        }// end function

        public function removeCollisionWith(param1:Pivot3D, param2:Boolean = true) : void
        {
            this._ray.removeCollisionWith(param1, param2);
            return;
        }// end function

        public function get camera() : Camera3D
        {
            return this._cam;
        }// end function

        public function set camera(param1:Camera3D) : void
        {
            this._cam = param1;
            return;
        }// end function

        public function get collisionTime() : int
        {
            return this._ray.collisionTime;
        }// end function

        public function get collisionCount() : int
        {
            return this._ray.collisionCount;
        }// end function

        public function get collided() : Boolean
        {
            return this._ray.collided;
        }// end function

        public function get ray() : RayCollision
        {
            return this._ray;
        }// end function

    }
}
